package com.icebergteam.warfacewidget.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import com.icebergteam.warfacewidget.core.LogManager;
import android.content.res.AssetManager;

public class FilesUtils {
	public static String className = FilesUtils.class.getCanonicalName();

	public static void copyDirectory(String sourceLocationStr,String targetLocationStr, List<String> ignoreList) {
		File sourceLocation = new File(sourceLocationStr);
		File targetLocation = new File(targetLocationStr);
		if (ignoreList != null
				&& ignoreList.contains(sourceLocation.getName())) {
				LogManager.LogVA(className, String.format(
					"File contains ignore list '%s'", sourceLocation));
			sourceLocation = null;
			targetLocation = null;
			return;
		}
		if (sourceLocation.isDirectory()) {
			String[] children = sourceLocation.list();
			LogManager.LogVA(className,
					"Is Directory? "
							+ String.format("Source '%s' count files = %s",
									sourceLocation, children.length));
			if (!targetLocation.exists()) {
				LogManager.LogVA(className,
						String.format(
								"Not exists '%s' directory into output target, was created",
								targetLocation));
				targetLocation.mkdirs();
			}

			
			for (int i = 0; i < children.length; i++) {
				LogManager.LogVA(className, String.format(
						"File list: source '%s' => target '%s'", sourceLocation
								+ File.separator + children[i], targetLocation
								+ File.separator + children[i]));
				copyDirectory((sourceLocationStr  + File.separator + children[i]), (
						targetLocationStr + File.separator + children[i]), ignoreList);
			}
			
			children = null;
			sourceLocation = null;
			targetLocation = null;
		} else {

			try {
				LogManager.LogVA(className, String.format(
						"Copy file: source '%s' => target '%s'",
						sourceLocation, targetLocation));
				copyFile(sourceLocationStr, targetLocationStr);
				sourceLocation = null;
				targetLocation = null;
			} catch (FileNotFoundException e) {
				LogManager.LogEA(className, e.getMessage());
			
			} catch (IOException e) {
				LogManager.LogEA(className, e.getMessage());
			}
		}
	}

	/**
	 * @param sourceLocation
	 * @param targetLocation
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void copyFile(String sourceLocationStr, String targetLocationStr)
			throws FileNotFoundException, IOException {
		File sourceLocation = new File(sourceLocationStr);
		File targetLocation = new File(targetLocationStr);
		InputStream in = new FileInputStream(sourceLocation);
		OutputStream out = new FileOutputStream(targetLocation);

		// Copy the bits from instream to outstream
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
		in = null;
		out = null;
		buf = null;
		sourceLocation = null;
		targetLocation = null;
	}

	static public boolean deleteDirectory(String pathStr) {
		File path = new File(pathStr);
		if (path.exists()) {
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i].getAbsolutePath());
				} else {
					files[i].delete();
				}
			}
			files = null;
		}
		return (path.delete());
	}

	public static void copyFile(InputStream in, OutputStream out)
			throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
		buffer = null;
	}

	/*copy directory from assets folder*/ 
	
	public static void copyDirectory(String sourceLocation,
			String targetLocation, AssetManager assetManager,
			List<String> ignoreList) {
		if (assetManager == null) {
			LogManager.LogEA(className, "AssetManager null object");
			return;
		}
		File sourceFile = new File(sourceLocation);
		if (ignoreList != null
				&& ignoreList.contains(sourceFile.getName())) {
			LogManager.LogVA(className, String.format(
					"File contains ignore list '%s'", sourceLocation));
			sourceFile = null;
			return;
		}
		sourceFile = null;
		String fileList[] = null;
		try {
			fileList = assetManager.list(sourceLocation);
			LogManager.LogVA(className,
					"Is Directory? "
							+ String.format("Source '%s' count files = %s",
									sourceLocation, fileList.length));
		} catch (IOException e1) {
			LogManager.LogEA(className, e1.getMessage());
		}
		File targetFile = new File(targetLocation);
		if (fileList.length > 0) {
			if (!targetFile.exists()) {
				LogManager.LogVA(className, 
						String.format(
								"Not exists '%s' directory into output target, was created",
								targetLocation));
				targetFile.mkdirs();

			}
			sourceLocation = ((sourceLocation.equals("")) ? sourceLocation: sourceLocation + File.separatorChar);
			for (int i = 0; i < fileList.length; i++) {
				LogManager.LogVA(className, String.format(
						"File list: source '%s' => target '%s'", sourceLocation
								+ fileList[i], targetLocation
								+ File.separator + fileList[i]));
			            	
			copyDirectory(sourceLocation  + fileList[i],
						(targetLocation + File.separator + fileList[i]),
						assetManager, ignoreList);
			}
			fileList = null;
		} else {
			InputStream in = null;
			OutputStream out = null;

			try {
				in = assetManager.open(sourceLocation);
				out = new FileOutputStream(targetLocation);
				LogManager.LogVA(className, String.format(
						"Copy file: source '%s' => target '%s'",
						sourceLocation, targetLocation));
				copyFile(in, out);
				in.close();
				out.close();
				in = null;
				out = null;

			} catch (IOException e) {
				LogManager.LogEA(className, e.getMessage());
				e.printStackTrace();
			}
		}
		targetFile = null;
	}

}